﻿using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Marx;
using ET;

namespace Demo
{
    public class UIStartup
    {
        public const string kName = "UIStartup";
        public const string kPath = "ui/main_panel";
    }

    [UIRegister(UIStartup.kName, UIStartup.kPath, UIType.Panel)]
    public class UIStartupController : UIController
    {
        UnityEngine.UI.Button button;
        protected override void OnInit()
        {
            button = GameObject.transform.Find("Panel/Button").GetComponent<UnityEngine.UI.Button>();
            button.onClick.AddListener(() => {
                if (!Enabled)
                {
                    return;
                }
                Log.Info("Button Clicked", LogColor.Red);
            });
            Log.Info((button == null).ToString());
        }

        protected override void OnAnimateEnter(Action onComplete)
        {
            //Global.Instance.StartCoroutine(AnimatorEnter(onComplete));
            Behaviour.StartCoroutine(AnimatorEnter(onComplete));
        }

        protected override void OnAnimateEnterStop()
        {
            Log.Info("Animate enter stoped");
            //Global.Instance.StopAllCoroutines();
        }

        private IEnumerator AnimatorEnter(Action complete)
        {
            yield return new WaitForSeconds(1);
            button.gameObject.SetActive(false);
            yield return new WaitForSeconds(1);
            button.gameObject.SetActive(true);
            yield return new WaitForSeconds(1);
            button.gameObject.SetActive(false);
            yield return new WaitForSeconds(1);
            button.gameObject.SetActive(true);
            yield return new WaitForSeconds(1);

            complete.Invoke();
        }

        protected override void OnEnter()
        {
            base.OnEnter();
            Log.Info("UIEntered");
        }

        protected override void OnAnimateLeave(Action onComplete)
        {
            Global.Instance.StartCoroutine(AnimatorEnter(onComplete));
        }

        protected override void OnLeave()
        {
            Log.Info("UILeaved");
        }
    }
}
